package org.example.redis_concurrent.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str,String formatDate) {
        if (formatDate == null|| formatDate.length()<8) {
            formatDate="yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatDate);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static String DateToFormatString(Date date,String formatDate) {
        if (formatDate == null|| formatDate.length()<8) {
            formatDate="yyyy-MM-dd hh:mm:ss";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatDate);
        String  dateString = null;
        dateString=format.format(date);
        return dateString;
    }

    public static void main(String[] args) {
        System.out.println(DateToFormatString(new Date(),null));
    }
}
